<?php

/**
 * Created by Vscode.
 * User: xiayu
 * Date: 2022/11/10
 * Time: 9:47
 */

namespace app\listener\wuyePay;

use app\logic\PayLogic;
use app\logic\PayRecordLogic;
use app\model\PayRecord;

 class PaySuccessPayRecordUpdateListener {

    /**
     * 支付成功回调
     * 对缴费记录进行修改
     * @param $detail
     */
    public function handle($detail)
    {
        ['order' => $order] = $detail;
        //转换参数
        $logic      = new PayLogic();
        $payService = $logic->payService();
        $result     = $payService->formatBackArgs($detail['result'] ?? []);

        $where['id'] = $order['product_id'];

        $record = PayRecord::withoutGlobalScope(['pid'])->where($where)->find();

        $data = [
            'order_no' => $order['order_no'],
            'status'   => 1
        ];

        PayRecord::withoutGlobalScope(['pid'])->where($where)->update($data);

        $payWhere['pid']        = $record['pid'];
        $payWhere['status']     = 0;

        $res = PayRecord::withoutGlobalScope(['pid'])->where($payWhere)->find();

        if(!$res){
            $resWhere['id'] = $record['pid'];
            $resData = [
                'status' => 1
            ];
            PayRecord::where($resWhere)->update($resData);
        }

    }
 }